package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.SellCarBean;
import com.project.mapper.ISellCarMapper;
import com.project.service.ISellCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SellCarServiceImpl implements ISellCarService {
    @Autowired
    private ISellCarMapper sellMapper;

    @Override
    public List<SellCarBean> findByUserId(Integer userId) {
        return sellMapper.findByUserId(userId);
    }

    @Override
    public void add(String productId, Integer userId) {
        QueryWrapper<SellCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_productId", productId);
        qw.eq("fk_userId", userId);
        SellCarBean sellCar = sellMapper.selectOne(qw);
        if (sellCar == null) {
            sellCar = new SellCarBean();
            sellCar.setProductId(productId);
            sellCar.setUserId(userId);
            sellMapper.insert(sellCar);
        } else {
            sellCar.setNum(sellCar.getNum() + 1);
            sellMapper.updateById(sellCar);
        }


    }

    @Override
    public void update(Integer sellCarId, Integer num) {
        SellCarBean sellCar = sellMapper.selectById(sellCarId);
        sellCar.setNum(num);
        sellMapper.updateById(sellCar);

    }

    @Override
    public void del(Integer sellCarId) {
        sellMapper.deleteById(sellCarId);
    }

    @Override
    public Integer getTotalPrice(Integer userId) {
        return sellMapper.getTotalPrice(userId);
    }
}
